import React, {Component} from 'react';
import '../css/other.css';
import url1 from '../image/icon/star1.png';
import url2 from '../image/icon/star2.png';
import url3 from '../image/icon/star3.png';

//评分星星列表
class Star extends Component{
    constructor(props){
        super(props);
        this.state = {
            point_list: [url3,url3,url3,url3,url3,]
        }
    }
    render(){
        return (
            <div className="starBox">
                {this.state.point_list.map((item,index) => {
                    return <img src={item} alt="" key={index} className="starImg"/>
                })}
                {this.props.point}
            </div>
        )
    }


    componentWillReceiveProps(nextProps){
        let po = nextProps.point;
        this.getList(po);
    }

    getList(po){
        let point_list = [];  //评分星星列表
        po = parseFloat(po);
        if(po >= 0.5){
            let point = po * 10;
            let start1 = parseInt(point/10);
            let start2 = point % 10;
            let start3 = 0;
            if (start2 >= 5) {
                start3 = 1
            }
            let start4 = 5 - start1 - start3;
            for(var i = 0; i<start1;i++){
                point_list.push(url1)
            }
            if (start3 == 1){
                point_list.push(url2)
            }
            if (start4>0){
                for (var j = 0; j < start4;j++){
                    point_list.push(url3)
                }
            }
        } else {
            for (var j = 0; j < 5; j++) {
                point_list.push(url3)
            }
        }
        this.setState({ point_list});
    }
}

export default Star;